﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DAL;
using DTO;
using Utility;

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Admin_CapNhatSanPham : System.Web.UI.Page
{

    private SqlConnection cn = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        string stringConnection = ConfigurationSettings.AppSettings["connectionstring"].ToString();
        cn = new SqlConnection(stringConnection);
        cn.Open();
        if (!IsPostBack)
        {
            LoadData();
            
        }
    }

    private void LoadData()
    {
           // buoc 1: lay id
            if (Request.QueryString["id"] == null)
            Response.Redirect("DanhSachSanPham.aspx");

            int id = int.Parse(Request.QueryString["id"]);
            txt_masp.Text = id.ToString();

            // buoc 2: load dropdownlist
            LoaiCollection loaicollection = LoaiBUS.GETALL();
            ddl_loaisp.DataSource = loaicollection;
            ddl_loaisp.DataTextField = "TenLoai";
            ddl_loaisp.DataValueField = "MaLoai";
            ddl_loaisp.DataBind();

            NhaSanXuatCollection NSXcollection = NhaSanXuatBUS.GETALL();
            ddl_nhasx.DataSource = NSXcollection;
            ddl_nhasx.DataTextField = "TenNhaSanXuat";
            ddl_nhasx.DataValueField = "MaNhaSanXuat";
            ddl_nhasx.DataBind();
             

            // buoc 3: load du lieu
            SanPham sp = SanPhamBUS.GetById(id);
            if (sp!=null)
            {
                txt_tensp.Text = sp.TenSanPham;
                txt_CPU.Text = sp.CPU;
                txt_ram.Text = sp.RAM;
                txt_hdd.Text = sp.HDD;
                txt_cardmanhinh.Text = sp.CardManHinh;
                txt_CD.Text = sp.CD_DVD;
                txt_manhinh.Text = sp.ManHinh;
                txt_mang.Text = sp.Mang;
                txt_tichhop.Text = sp.TichHop;
                txt_trongluong.Text = sp.TrongLuong;
                txt_pin.Text = sp.Pin;
                txt_hdh.Text = sp.HeDieuHanh;
                txt_tgia_bhanh.Text = sp.ThoiGianBaoHanh;
                txt_khuyenmai.Text = sp.KhuyenMai;
                txt_gia.Text = sp.Gia.ToString();
                ddl_nhasx.SelectedValue = sp.MaNhaSanXuat.ToString();

                ddl_loaisp.SelectedValue = sp.MaLoai.ToString();

                HinhAnhCollection hinhanh = HinhAnhBUS.GetById(sp.MaSanPham);
                repeater_hinhanh.DataSource = hinhanh;
                repeater_hinhanh.DataBind();
                
            }
            else
            {
                Response.Redirect("~/Admin/DanhSachSanPham.aspx");
            }

        
    }

    protected void repeater_hinhanh_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            HinhAnh hinh = (HinhAnh)e.Item.DataItem;
            FileUpload FileUpload1 = (FileUpload)e.Item.FindControl("FileUpload1");

            ImageMap img_map = (ImageMap)e.Item.FindControl("img_map");
            img_map.ImageUrl = "~/Photos/" + hinh.DuongDan;
        }
    }


    protected void btn_sua_Click(object sender, EventArgs e)
    {
            int id = int.Parse(Request.QueryString["id"]);
            SanPham sp = SanPhamBUS.GetById(id);
            if (sp != null)
            {
                sp.MaSanPham = int.Parse(txt_masp.Text);
                sp.TenSanPham = txt_tensp.Text;
                sp.MaLoai = int.Parse(ddl_loaisp.SelectedValue);
                sp.CPU = txt_CPU.Text;
                sp.RAM = txt_ram.Text;
                sp.HDD = txt_hdd.Text;
                sp.CardManHinh = txt_cardmanhinh.Text;
                sp.CD_DVD = txt_CD.Text;
                sp.ManHinh = txt_manhinh.Text;
                sp.Mang = txt_mang.Text;
                sp.TichHop = txt_tichhop.Text;
                sp.TrongLuong = txt_trongluong.Text;
                sp.Pin = txt_pin.Text;
                sp.HeDieuHanh = txt_hdh.Text;
                sp.ThoiGianBaoHanh = txt_tgia_bhanh.Text;
                sp.KhuyenMai = txt_khuyenmai.Text;
                sp.Gia = int.Parse(txt_gia.Text);
                sp.MaNhaSanXuat = int.Parse(ddl_nhasx.SelectedValue.ToString());
                sp.MaLoai = int.Parse(ddl_loaisp.SelectedValue.ToString());
                SanPhamBUS.UPDATE(sp);

                HttpFileCollection httpFileCollection = Request.Files;
                for (int i = 0; i < httpFileCollection.Count; i++)
                {

                    HttpPostedFile httpPostedFile = httpFileCollection[i];
                    if (httpPostedFile.ContentLength > 0)
                    {


                        HinhAnh hinhanh = new HinhAnh();
                        hinhanh.MaSanPham = id;

                        string filename = DateTime.Now.Ticks.ToString() + "_" + httpPostedFile.FileName;
                        httpPostedFile.SaveAs(Server.MapPath("../Photos/") + filename);
                        System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("../Photos/") + filename);
                        ImageUtility.MakeThumbnailPhoto(img, 100, 110, Server.MapPath("../Photos/thumbs/"), filename);
                        hinhanh.DuongDan = filename;
                        HinhAnhBUS.UPDATE(hinhanh);
                    }

                    Response.Redirect("~/Admin/DanhSachSanPham.aspx");
                }
            }
    }
}